home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0896.zip / KREHBIEL.ZIP / VBE.C < prev    next >
C/C++ Source or Header  |  1996-04-15  |  4KB  |  145 lines

  1. Listing 1:
  2. /*--------------------------------------------------------------
  3.   vbe.c            VESA Super VGA Interface
  4. ----------------------------------------------------------------
  5.  
  6.   Platform: IBMPC/MSDOS.
  7.   
  8.   Functions:
  9.       VbeGetVbeInfo    Return VBE information
  10.       VbeGetModeInfo    Return VESA mode information
  11.       VbeSetMode        Initialize video mode
  12.       VbeSetPalette    Load DAC palette
  13.       VbeSetWindow    Move memory window
  14.       VbeWrite        Copy buffer to display
  15.   
  16.   Edit history:
  17.     04/12/96    C.Krehbiel.  Implemented VBE 2.0 interface in
  18.                 MSVC 1.0, medium memory model.
  19. --------------------------------------------------------------*/
  20.  
  21. #include <string.h>
  22.  
  23. #include <dos.h>
  24.  
  25. #include "vbe.h"    /* exports and data types */
  26.  
  27. /*--------------------------------------------------------------
  28.                          local data
  29. --------------------------------------------------------------*/
  30.  
  31. static VbeInfo_t _VbeInfo;        /* current vbe info */
  32. static ModeInfo_t _ModeInfo;    /* current mode info */
  33.  
  34. static int _Width;            /* scan line width in bytes */
  35. static long _Window;        /* memory window size in bytes */
  36. static int _Segment;        /* memory window segment */
  37.  
  38. /*--------------------------------------------------------------
  39.                       exported functions
  40. --------------------------------------------------------------*/
  41. int VbeGetVbeInfo(VbeInfo_t far *p)
  42.  
  43.     /* fetches the vbe info block; returns 0 if no vbe. */
  44. {
  45.     union REGS r={0x4f00,0,0,0,0,FP_OFF(p)};
  46.     struct SREGS s={FP_SEG(p)};
  47.  
  48.     if(!p) return 0;
  49.         
  50.     _fmemset(p,0,sizeof(VbeInfo_t));
  51.     _fmemcpy(p->VbeSignature,"VBE2",4);
  52.     int86x(0x10,&r,&r,&s);
  53.  
  54.     if(_fmemcmp(p->VbeSignature,"VESA",4)) return 0;
  55.     return r.x.ax==0x4f;
  56. }
  57. int VbeGetModeInfo(int mode,ModeInfo_t far *p)
  58.  
  59.     /* fetches the mode info block for the specified mode 
  60.     number;    returns 0 if mode unsupported by vbe. */
  61. {
  62.     union REGS r={0x4f01,0,mode,0,0,FP_OFF(p)};
  63.     struct SREGS s={FP_SEG(p)};
  64.  
  65.     if(!p) return 0;
  66.     
  67.     _fmemset(p,0,sizeof(ModeInfo_t));
  68.     int86x(0x10,&r,&r,&s);
  69.  
  70.     return r.x.ax==0x4f;
  71. }
  72. int VbeSetMode(int mode)
  73.  
  74.     /* initializes the requested video mode; returns 0 if 
  75.     there's no vbe or the mode is unavailable. */
  76. {
  77.     union REGS r={0x4f02,mode};
  78.  
  79.     if(!VbeGetVbeInfo(&_VbeInfo)) return 0;
  80.     
  81.     if(mode>=0x100)    /* svga mode */
  82.     {
  83.         if(!VbeGetModeInfo(mode,&_ModeInfo) || 
  84.             !(_ModeInfo.ModeAttributes&1)) return 0;
  85.     
  86.         _Width=_ModeInfo.BytesPerScanLine;
  87.         _Window=1024L*_ModeInfo.WinSize;    /* convert to bytes */
  88.         _Segment=_ModeInfo.WinASegment;        
  89.     }
  90.     int86(0x10,&r,&r);
  91.     
  92.     return r.x.ax==0x4f;
  93. }
  94. void VbeSetPalette(const char far *p,int start,int n)
  95.  
  96.     /* loads the dac palette registers; uses bios on vbe 
  97.     versions before 2.0 */
  98. {
  99.     if(_VbeInfo.VbeVersion<0x200)    /* use bios */
  100.     {
  101.         union REGS r={0x1012,start,n,FP_OFF(p)};
  102.            struct SREGS s={FP_SEG(p)};
  103.            int86x(0x10,&r,&r,&s);
  104.     }
  105.     else    /* use vbe */
  106.     {
  107.         union REGS r={0x4f09,0,n,start,0,FP_OFF(p)};
  108.            struct SREGS s={FP_SEG(p)};
  109.            int86x(0x10,&r,&r,&s);
  110.     }
  111. }
  112. void VbeSetWindow(int window,int position)
  113.  
  114.     /* repositions the indicates memory window to the new
  115.     position (in WinGranularity units). */
  116. {
  117.     union REGS r={0x4f05,window,0,position};
  118.     int86(0x10,&r,&r);
  119. }
  120. void VbeWrite(int x,int y,int bytes,const char far *buffer)
  121.  
  122.     /* copies the contents of the buffer (<64k) to display, 
  123.     starting at pixel (x,y). */
  124. {
  125.     long absolute=x+(long)y*_Width;     /* absolute offset */
  126.     long position=absolute/_Window;        /* of window */
  127.     long offset=absolute%_Window;        /* of window */
  128.     char far *vram=MK_FP(_Segment,0);    /* to window */
  129.  
  130.     VbeSetWindow(0,(int)position);
  131.     
  132.     if(offset+bytes>_Window)    /* data overruns window */
  133.     {
  134.         int    n=(int)(_Window-offset);    /* bytes left */
  135.         
  136.            _fmemcpy(vram+offset,buffer,n);    /* display 1st part */
  137.         VbeSetWindow(0,(int)++position);/* move window */
  138.         _fmemcpy(vram,buffer+n,bytes-n);/* display rest */
  139.     }    
  140.     else _fmemcpy(vram+offset,buffer,bytes);/* no overrun */
  141. }
  142. /*--------------------------------------------------------------
  143.                               eof
  144. --------------------------------------------------------------*/
  145.